package pers.zxk.jlytnews.manager;

import jdk.nashorn.internal.ir.RuntimeNode;
import org.omg.CORBA.Request;
import pers.zxk.jlytnews.entity.User;

import javax.servlet.http.HttpServletRequest;

public class UserStateManager {

    public static final String ADMIN = "admin";
    public static final String USERNAME = "username";

    public static boolean isLogin(HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(USERNAME);
        if (attribute == null)
            return false;
        String username = (String) attribute;
        if (username != null) {
            return true;
        }
        return false;
    }

    public static boolean isAdmin(HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(ADMIN);
        if (attribute == null)
            return false;

        int admin = (int) attribute;
        if (admin == 1) {
            return true;
        } else {
            return false;
        }
    }

    public static void setLogin(HttpServletRequest request, User user) {
        request.getSession().setAttribute(USERNAME, user.getUsername());
        request.getSession().setAttribute(ADMIN, user.getAdmin());
    }

    public static void setLogout(HttpServletRequest request) {
        request.getSession().removeAttribute(USERNAME);
        request.getSession().removeAttribute(ADMIN);
    }
}
